package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.constant.SysConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.AccountUserLastPos;
import com.tbit.uqbike.object.pojo.TerPosition;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.AccountUserLastPosService;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.base.HistoryService;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.GeoRegionService;
import com.tbit.uqbike.webmanager.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * 运维轨迹
 *
 * @author LMY
 * @create 2021-03-22 17:04
 */
@Slf4j
@RestController
@RequestMapping("/accountUserIdController")
public class AccountUserHistoryController {

    @Autowired
    private HistoryService historyService;
    @Autowired
    private AccountUserLastPosService accountUserLastPosService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private GeoRegionService geoRegionService;

    /**
     * 根据时间查询单个运维轨迹
     */
    @RequestMapping("/getByTime")
    public JsonResult getByTime(Integer accountUserId, String startTime, String endTime, Integer mapType) {
        Assert.notNull(startTime, "System.startTime.notNull");
        Assert.notNull(endTime, "System.endTime.notNull");
        Assert.notNull(accountUserId, "AccountUser.id.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        AccountUser accountUser = accountUserService.getAccountUserById(accountUserId);

        if (accountUser == null) {
            throw new BaseException("AccountUser.notExist");
        }

        List<TerPosition> terPositions = historyService.getByAccountUserPhoneAndTime(mapType, accountUser.getPhone(), startTime, endTime);

        return JsonResult.ok(terPositions);
    }

    /**
     * 查询单个运维最后位置
     */
    @RequestMapping("/lastPos")
    public JsonResult lastPos(Integer accountUserId, Integer mapType) {
        Assert.notNull(accountUserId, "AccountUser.notExist");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        AccountUserLastPos accountUserLastPos = accountUserLastPosService.getByAccountUserId(accountUserId);

        if (accountUserLastPos != null) {
            calibrateService.calibrateLastPos(Collections.singletonList(accountUserLastPos), mapType);
        }

        return JsonResult.ok(accountUserLastPos);
    }

    /**
     * 查询所有运维最后位置
     */
    @RequestMapping("/allLastPos")
    public JsonResult allLastPos(String token, Integer accountId, Integer mapType,
                                 @RequestParam(value = "regionId", required = false) List<Integer> regionId,
                                 Integer accountUserId,
                                 Integer pageNO,Integer rowCount,
                                 HttpServletRequest request) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");

        log.info("accountId:{},regionId:{}",accountId, JSON.toJSONString(regionId));

        LoginInfo login = BaseController.getLogin(request);

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        if (CollUtil.isEmpty(regionId)) {
            regionId = geoRegionService.getByAccountUserId(login.getAccountUserId());
        }

        List<AccountUserLastPos> accountUserLastPos = accountUserLastPosService.getByAccountId(accountId, login.getType(), login.getAccountUserId(), regionId,accountUserId,pageNO,rowCount);

        if (accountUserLastPos != null) {
            calibrateService.calibrateLastPos(accountUserLastPos, mapType);
        }

        return JsonResult.ok(accountUserLastPos);
    }

}
